package com.colin.jipay;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import com.colin.jipay.activity.MainActivity;
import com.colin.jipay.config.ActConst;
import com.colin.jipay.utils.StringUtils;

/**
 * author    : colin
 * e-mail    : colin12138@gmail.com
 * date      : 2019/6/25 16:24
 * qq        : 1018294974
 */
public class NotificationService extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = "#"+extras.getString(Notification.EXTRA_TEXT);
        Log.i("Test", "Notification posted " + notificationTitle + " & " + notificationText);
        if (notificationTitle == null)
            return;
        if (notificationTitle.contains("支付宝通知")){
            if (notificationText.contains("通过扫码向你付款")){
                Intent intent = new Intent();
                intent.setAction(ActConst.NOTIFICATION_POST);
                intent.putExtra("payer",StringUtils.getTextCenter(notificationText,"#","通过"));
                intent.putExtra("money",StringUtils.getTextCenter(notificationText,"付款","元"));
                sendBroadcast(intent);
            }
        }


    }

    @Override
    public void onCreate() {
        super.onCreate();
        MainActivity.sendmsg("服务启动");
        Log.d("---","服务启动");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MainActivity.sendmsg("服务被杀掉 正在重启");
        Log.d("---","服务被杀掉 正在重启");
        Intent intent = new Intent(getApplicationContext(),NotificationService.class);
        startService(intent);
    }
}
